package com.example.designhbwxz.book.service.impl;

import com.example.designhbwxz.book.constant.EnumConstants;
import com.example.designhbwxz.book.factory.RegisterLoginComponentFactory;
import com.example.designhbwxz.book.pojo.UserInfo;
import com.example.designhbwxz.book.repo.UserRepository;
import com.example.designhbwxz.book.service.UserBridgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户业务层实现类
 *
 * @author shenzw
 * @date 2023/10/23
 */
@Service
public class UserBridgeServiceImpl implements UserBridgeService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 用户登陆
     *
     * @param account  账号
     * @param password 密码
     * @return String
     */
    @Override
    public String login(String account, String password) {
        // 用左路的具体子类创建调用入口
        return RegisterLoginComponentFactory.getComponent(EnumConstants.DEFAULT.getCode())
                .login(account, password);
    }

    /**
     * 注册用户
     *
     * @param userInfo 注册用户信息
     * @return String
     */
    @Override
    public String register(UserInfo userInfo) {
        return RegisterLoginComponentFactory.getComponent(EnumConstants.DEFAULT.getCode())
                .register(userInfo);
    }

    /**
     * 校验账号是否已经存在，即注册过
     *
     * @param userName 账号
     * @return boolean
     */
    @Override
    public boolean checkUserExists(String userName) {
        UserInfo byUserName = userRepository.findByUserName(userName);
        return byUserName != null;
    }

    /**
     * 第三方登陆
     *
     * @param request HttpServletRequest
     * @param type    GITEE
     * @return String
     */
    @Override
    public String login3rd(HttpServletRequest request, String type) {
        return RegisterLoginComponentFactory.getComponent(type)
                .login3rd(request);
    }
}
